#include "network.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//打开文件
MODULE_ERRPO module_file_open(ATModuleConfig* cfg,const char* filename,int* handle)
{
    if(cfg)
    {
        AT_ERROR err;
        char ucOpenfileBuff[128];
        const char *recBuff;
        snprintf(ucOpenfileBuff,128,"AT+QFOPEN=\"%s\",0\r\n",filename);
        if(cfg->eModuleStatus != AT_MODULE_IDLE)
            return MODULE_ERR_NO_INIT;
        err = at_parser_send_wait(&cfg->atConfig,
                                ucOpenfileBuff,
                                strlen(ucOpenfileBuff),
                                "+QFOPEN:",
                                &recBuff,
                                DEFAULT_TIMEOUT,
                                1
                                );
        if(err == AT_ERR_NONE)
        {
            const char* openHead = strstr(recBuff,"+QFOPEN");
            if(openHead)
            {
                sscanf(openHead,"+QFOPEN: %d",handle);
                return MODULE_ERR_NONE;
            }
        }
    }
    return MODULE_ERR_PARAM;
}

//读取文件
MODULE_ERRPO module_file_read(ATModuleConfig* cfg,int handle,int offset,int readlen,int* reallen,unsigned char* buff)
{
    if(cfg)
    {
        AT_ERROR err;
        char ucSeekfileBuff[128];
        snprintf(ucSeekfileBuff,128,"AT+QFSEEK=%d,%d\r\n",handle,offset);
        if(cfg->eModuleStatus != AT_MODULE_IDLE)
            return MODULE_ERR_NO_INIT;
        err = at_parser_send_wait(&cfg->atConfig,
                                ucSeekfileBuff,
                                strlen(ucSeekfileBuff),
                                "OK",
                                0,
                                DEFAULT_TIMEOUT,
                                1
                                );
        if(err == AT_ERR_NONE)
        {
            snprintf(ucSeekfileBuff,128,"AT+QFREAD=%d,%d\r\n",handle,readlen);
			
			err = at_parser_send(&cfg->atConfig,ucSeekfileBuff,strlen(ucSeekfileBuff));
            if(err == AT_ERR_NONE)
            {
                int buffReadlen = 0;
				unsigned char* tmpBuff = (unsigned char*)malloc(readlen+100);
				err = at_parser_read(&cfg->atConfig,tmpBuff,&buffReadlen,readlen+100,DEFAULT_TIMEOUT);
				if(err == AT_ERR_NONE)
				{
					const char* connectHead = strstr((const char*)tmpBuff,"CONNECT");
					if(connectHead)
					{
						if(1 == sscanf(connectHead,"CONNECT %d",reallen))
						{
							const char* dataHead = strstr(connectHead,"\r\n");
							memcpy(buff,dataHead + 2,*reallen);
							free(tmpBuff);
							return MODULE_ERR_NONE;
						}
					}
				}
				free(tmpBuff);
            }
            
        }
    }
    return MODULE_ERR_PARAM;
}

//关闭文件
MODULE_ERRPO module_file_close(ATModuleConfig* cfg,int handle)
{
    if(cfg)
    {
        AT_ERROR err;
        char ucClosefileBuff[128];
        snprintf(ucClosefileBuff,128,"AT+QFCLOSE=%d\r\n",handle);
        if(cfg->eModuleStatus != AT_MODULE_IDLE)
            return MODULE_ERR_NO_INIT;
        err = at_parser_send_wait(&cfg->atConfig,
                                ucClosefileBuff,
                                strlen(ucClosefileBuff),
                                "OK",
                                0,
                                DEFAULT_TIMEOUT,
                                1
                                );
        if(err == AT_ERR_NONE)
        {
            return MODULE_ERR_NONE;
        }
    }
    return MODULE_ERR_PARAM;
}
